<script setup lang="ts">
  import { ref, computed, watch, reactive, onMounted } from 'vue';
  import { useBooleanStore } from '@/store';
  import { erpOutWorkSearch } from '@/api/inventory-management';
  import { ERPOutSearchParam } from '@/api/inventory-management/types';
  import { Notification } from '@arco-design/web-vue';

  const booleanStore = useBooleanStore();
  const emit = defineEmits(['updateArray']);
  const pageSize = ref<number>(10);
  const pageNumber = ref<number>(1);
  const formValues = reactive({
    workbinCode: '',
    mProductNumber: '',
    barcode: '',
    itemCode: '',
    qualityCode: '',
  });
  // 发送搜索数据
  const flowQueryData = computed<ERPOutSearchParam>(() => ({
    searchParams: {
      itemCode: formValues.itemCode,
      mProductNumber: formValues.mProductNumber,
      qualityCode: formValues.qualityCode,
      workbinCode: formValues.workbinCode,
      barcode: formValues.barcode,
    },
    pageParameter: {
      rows: pageSize.value,
      page: pageNumber.value,
    },
  }));
  async function handleSearch() {
    const newData = await erpOutWorkSearch(flowQueryData.value);
    if (newData) {
      emit('updateArray', newData);
    } else {
      Notification.error({
        title: '失败',
        content: newData.message ? newData.message : '查询失败',
        closable: true,
      });
    }
  }
  function reset() {
    Object.keys(formValues).forEach((key) => {
      (formValues as Record<string, any>)[key] = '';
    });
    setTimeout(() => {
      handleSearch();
    }, 150);
  }
  function outTaskLocal() {
    booleanStore.toggleBool2(8);
  }
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 34) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
  watch(
    () => booleanStore.update,
    () => {
      if (booleanStore.num6 === 16) handleSearch();
    }
  );
  onMounted(() => {
    handleSearch();
  });
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
    <a-col :span="6">
      <a-input
        v-model="formValues.workbinCode"
        placeholder="缓存箱编号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.mProductNumber"
        placeholder="成品型号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.barcode"
        placeholder="条码"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.itemCode"
        placeholder="图号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
    <a-col :span="6">
      <a-input
        v-model="formValues.qualityCode"
        placeholder="质量编号"
        size="large"
        @press-enter="handleSearch"
      />
    </a-col>
  </a-row>
  <div>
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
    <a-button
      type="outline"
      class="btn-reset"
      size="large"
      @click="outTaskLocal"
      >查询当前位置的出库任务</a-button
    >
  </div>
</template>

<style scoped>
  span {
    margin-bottom: 16px;
  }
  .btn-reset {
    margin-left: 10px;
  }
</style>
